/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.netease.xone.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.actionbarsherlock.view.Window;
import com.netease.xone.R;

public class ActivityMovieView extends ActivityX1Base {

    private MovieViewControl mControl;
    private boolean mFinishOnCompletion;
    private boolean mResumed = false;
    private boolean mFocused = false;
    private boolean mControlResumed = false;

    public static void playVideo(Context context, String uriString) {
        Uri uri = Uri.parse(uriString);
        Intent intent = new Intent();
        if (!(context instanceof Activity)) {

            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        intent.setClass(context, ActivityMovieView.class);
        intent.setData(uri);
        context.startActivity(intent);
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_video);
        View rootView = findViewById(R.id.root);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        Intent intent = getIntent();
        mControl = new MovieViewControl(rootView, this, intent.getData()) {

            @Override
            public void onCompletion() {
                if (mFinishOnCompletion) {
                    finish();
                }
            }
            
            @Override
            public boolean onError(MediaPlayer player, int arg1, int arg2) {
                super.onError(player, arg1, arg2);
                Toast.makeText(ActivityMovieView.this, R.string.video_play_error, Toast.LENGTH_SHORT).show();
                finish();
                return false;
            }
        };
        if (intent.hasExtra(MediaStore.EXTRA_SCREEN_ORIENTATION)) {
            int orientation = intent.getIntExtra(MediaStore.EXTRA_SCREEN_ORIENTATION,
                    ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
            if (orientation != getRequestedOrientation()) {
                setRequestedOrientation(orientation);
            }
        }
        mFinishOnCompletion = intent.getBooleanExtra(MediaStore.EXTRA_FINISH_ON_COMPLETION, true);
    }
    @Override
    protected void requestFeature() {
        // 设置全屏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    @Override
    public void onResume() {
        super.onResume();
        mResumed = true;
        if (mFocused && mResumed && !mControlResumed) {
            mControl.onResume();
            mControlResumed = true;
        }
    }
    
    @Override
    protected void setOrientation() {
        /**
         * 设置屏幕为横屏
         */
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
    
    @Override
    public void onPause() {
        super.onPause();
        mResumed = false;
        if (mControlResumed) {
            mControl.onPause();
            mControlResumed = false;
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        mFocused = hasFocus;
        if (mFocused && mResumed && !mControlResumed) {
            mControl.onResume();
            mControlResumed = true;
        }
    }
}
